package com.rt.code.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rt.code.bean.A1templatetable;
import com.rt.code.dto.A1templatecolumnDto;
import com.rt.code.dto.PageDto;
import com.rt.code.mapper.A1templatecolumnMapper;
import com.rt.code.mapper.A1templatetableMapper;
import com.rt.code.service.intf.IA1templatecolumnService;
import com.rt.tmpt.bean.A1TemplateColumn;
import com.rt.tmpt.dto.Result;
import com.rt.tmpt.service.impl.DataLimitService;
import com.rt.tmpt.utils.DateTool;
import com.rt.code.bean.A1templatecolumn;
import com.rt.easyjava.common.config.dozer.MyMapper;
import com.rt.code.utils.page.MyPageUtils;

import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wxy on 2016/7/28.
 */
@Service(value="a1templatecolumnService")
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class A1templatecolumnService extends ServiceImpl<A1templatecolumnMapper, A1templatecolumn> implements IA1templatecolumnService {


    @Autowired
    private DataLimitService dataLimitService;
    @Autowired
    private MyMapper mapper;
    @Autowired
    private MyPageUtils myPageUtils;
    @Resource
    private A1templatecolumnMapper a1templatecolumnMapper;
    @Resource
    private A1templatetableMapper a1templatetableMapper;


    @Override
    public Result<A1templatecolumnDto> pagination(A1templatecolumnDto a1templatecolumnDto, PageDto pageDto) {
        //分页查询
        Result<A1templatecolumnDto> result = myPageUtils.pagination(
                a1templatecolumnDto,pageDto,
                A1templatecolumn.class,
                A1templatecolumnDto.class,
                a1templatecolumnMapper,
                false,null);

        return result;
    }

    /**
    * 分布查询，有数据边界控制功能
    * @param a1templatecolumnDto
    * @param pageDto
    * @param paramMap
    * @return
    */
    @Override
    public Result<A1templatecolumnDto> pagination(A1templatecolumnDto a1templatecolumnDto, PageDto pageDto, Map<String,Object> paramMap) {
        //数据边界控制参数
        Map<String,Object> paramMap1 = dataLimitService.getLimitParam(paramMap);

        //分页查询
        Result<A1templatecolumnDto> result = myPageUtils.pagination(
                a1templatecolumnDto,pageDto,
                A1templatecolumn.class,
                A1templatecolumnDto.class,
                a1templatecolumnMapper,
                paramMap1,
                false,null);

        return result;
    }


    @Override
    public Result<A1templatecolumnDto> selectAll() {
        Result<A1templatecolumnDto> result = new Result();

        //排序
        PageDto pageDto = new PageDto();
        pageDto.setSortColumn("id");
        pageDto.setSortOrder("desc");
        Map<String,Object> mapParam = new HashMap<>();
        mapParam.put("page",pageDto);

        List<A1templatecolumn> list = a1templatecolumnMapper.mySelectAll(mapParam);
        List<A1templatecolumnDto> dtoList = mapper.maplist(list, A1templatecolumnDto.class);
        result.setList(dtoList);

        return result.success();
    }

    @Override
    public Result<A1templatecolumnDto> selectById(long id) {
        Result<A1templatecolumnDto> result = new Result();

        A1templatecolumn a1templatecolumn = a1templatecolumnMapper.mySelectById(id);
        A1templatecolumnDto dto = mapper.map(a1templatecolumn, A1templatecolumnDto.class);
        result.setObj(dto);

        return result.success();
    }


    /**
        返回ID值
    */
    @Override
    public Result<A1templatecolumnDto> addData(A1templatecolumnDto a1templatecolumnDto){
        Result<A1templatecolumnDto> result = new Result();

        //封闭数据
        A1templatecolumn a1templatecolumn = mapper.map(a1templatecolumnDto, A1templatecolumn.class);
        a1templatecolumn.setCreatedate(DateTool.getCurTimestamp());

        //调用mapper
        long i = a1templatecolumnMapper.myInsert(a1templatecolumn);
        if(i==0){
            return result.fault("插入失败");
        }

        A1templatecolumnDto dto = mapper.map(a1templatecolumn, A1templatecolumnDto.class);
        result.setObj(dto);

        return result.success();
    }

    /**
        返回ID值
    */
    @Override
    public Result<A1templatecolumnDto> editData(A1templatecolumnDto a1templatecolumnDto){
        Result<A1templatecolumnDto> result = new Result();

        //封装数据
        A1templatecolumn a1templatecolumn = mapper.map(a1templatecolumnDto, A1templatecolumn.class);
        a1templatecolumn.setModifydate(DateTool.getCurTimestamp());

        //调用mapper
        long i = a1templatecolumnMapper.myUpdateById(a1templatecolumn);
        if(i==0){
            return result.fault("编辑数据失败");
        }

        A1templatecolumnDto dto = mapper.map(a1templatecolumn, A1templatecolumnDto.class);
        result.setObj(dto);

        return result.success();
    }


    /**
     * 批量修改数据
     * @param a1templatecolumnDtoList
     * @return
     */
    @Override
    public Result editDataList(List<A1templatecolumnDto> a1templatecolumnDtoList) {
        Result result = new Result();

        List<A1templatecolumn> a1templatecolumnList = mapper.maplist(a1templatecolumnDtoList, A1templatecolumn.class);

        for(A1templatecolumn a1templatecolumn: a1templatecolumnList){
            long i = a1templatecolumnMapper.myUpdateById(a1templatecolumn);
            if(i==0){
                throw new RuntimeException("批量操作失败");
            }
        }

        return result.success();
    }


    /**
     * 批量修改数据
     * @param ids
     * @return
     */
    @Override
    public Result editColumnSortByIds(List<Long> ids) {
        Result result = new Result();

        int i=1;
        for (Long id: ids) {
            A1templatecolumn column = a1templatecolumnMapper.selectById(id);
            column.setSort(i++);
            a1templatecolumnMapper.updateById(column);
        }

        return result.success();
    }


    /**
    伪删除记录，返回ID值
    */
    @Override
    public Result deleteById(long id, String adminMan){
        Result result = new Result();

        Map<String,Object> mapParameter = new HashMap();
        mapParameter.put("id",id);
        mapParameter.put("modifyman",adminMan);
        mapParameter.put("modifydate", DateTool.getCurTimestamp());

        //调用mapper
        long i = a1templatecolumnMapper.myDeleteById(mapParameter);
        if(i==0){
            return result.fault("删除数据失败");
        }

        return result.success();
    }




    /**
    *页面上查询列的值，动态加载，sql语句读数据库配置而来
    */
    @Override
    public List select4a1templatetableId(){
        return a1templatecolumnMapper.select4a1templatetableId();
    }


    /**
     * 更新模板字段值;  如:更新列宽
     * 通过， a1templatecolumnDto.columnname 和 keyrandom 来定位， templatecolumn
     * @param a1templatecolumnDto
     * @param keyRandom
     * @return
     */
    @Override
    public Result updateTmptColumn(A1templatecolumnDto a1templatecolumnDto, String keyRandom){
        if (a1templatecolumnDto.getColumnname() == null) {
            return Result.getInstance().fault("该宽调整无效");
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("keyrandom", keyRandom);
        queryWrapper.eq("deleteflag", 0);
        A1templatetable a1templatetable = a1templatetableMapper.selectOne(queryWrapper);

        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("tmpt_table_id", a1templatetable.getId());
        queryWrapper1.eq("columnname", a1templatecolumnDto.getColumnname());
        queryWrapper1.eq("deleteflag", 0);
        A1templatecolumn a1templatecolumn = a1templatecolumnMapper.selectOne(queryWrapper1);

        a1templatecolumn.setColumnwidth(new BigDecimal(a1templatecolumnDto.getColumnwidth()).intValue()); // 更新列宽
        // 更新其它值，以后按需加
        a1templatecolumnMapper.updateById(a1templatecolumn);

        return Result.getInstance().success();
    }

}
